package com.lzw.mysqldemo.mypool;

import org.apache.commons.pool2.impl.GenericObjectPool;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

public class RealObjectFactory<T> {

    //创建资源
    public LzwPoolObject<T> mackObject(){
        //模拟珍贵资源的创建过程
        try {
            Thread.sleep(200);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        LzwObject lzwObject = new LzwObject();
        LzwPoolObject lzwPoolObject = new LzwPoolObject(lzwObject);
        return lzwPoolObject;
    }

    //检查资源
    public boolean validateObject(LzwPoolObject<T> lzwPoolObject){
        //检测资源的健康状态，比如： 数据库连接可以 select 1;
        return false;
    }


    //销毁资源
    public void destoryObject(LzwPoolObject<T> lzwPoolObject){
        //比如： 关闭链接
    }

//    public static BlockingQueue<Integer> vals = new LinkedBlockingQueue<>();
//    public static AtomicInteger atomicInteger = new AtomicInteger(0);

//    public static void main(String[] args) throws InterruptedException {
//        CountDownLatch countDownLatch = new CountDownLatch(1);
//        CountDownLatch countDownLatch2 = new CountDownLatch(10000);
//        ExecutorService es = Executors.newFixedThreadPool(100);
//        for(int i =0;i<10000;i++){
//            es.execute(new Runnable() {
//                @Override
//                public void run() {
//                    try {
//                        countDownLatch.await();
//                    } catch (InterruptedException e) {
//                        e.printStackTrace();
//                    }
//                    if(vals.size() < 100){
//                        try {
//                            Thread.sleep(100);
//                        } catch (InterruptedException e) {
//                            e.printStackTrace();
//                        }
//                        vals.add(1);
//                    }
//                    countDownLatch2.countDown();
//                }
//            });
//        }
//        Thread.sleep(2000);
//        countDownLatch.countDown();
//        countDownLatch2.await();
//        System.out.println(vals.size());
//    }

//    public static void main(String[] args) throws InterruptedException {
//        CountDownLatch countDownLatch = new CountDownLatch(1);
//        CountDownLatch countDownLatch2 = new CountDownLatch(10000);
//        ExecutorService es = Executors.newFixedThreadPool(100);
//        for(int i =0;i<10000;i++){
//            es.execute(new Runnable() {
//                @Override
//                public void run() {
//                    try {
//                        countDownLatch.await();
//                    } catch (InterruptedException e) {
//                        e.printStackTrace();
//                    }
//                    synchronized (RealObjectFactory.class) {
//                        if (vals.size() < 100) {
//                            try {
//                                Thread.sleep(100);
//                            } catch (InterruptedException e) {
//                                e.printStackTrace();
//                            }
//                            vals.add(1);
//                        }
//                    }
//                    countDownLatch2.countDown();
//                }
//            });
//        }
//        Thread.sleep(2000);
//        countDownLatch.countDown();
//        long starttime = System.currentTimeMillis();
//        countDownLatch2.await();
//        System.out.println("time:"+(System.currentTimeMillis()-starttime));
//        System.out.println(vals.size());
//    }
//
//    public static void main(String[] args) throws InterruptedException {
//        CountDownLatch countDownLatch = new CountDownLatch(1);
//        CountDownLatch countDownLatch2 = new CountDownLatch(10000);
//        CountDownLatch countDownLatch3 = new CountDownLatch(5000);
//        CountDownLatch countDownLatch4 = new CountDownLatch(1);
//        ExecutorService es = Executors.newFixedThreadPool(100);
//        for(int i =0;i<10000;i++){
//            es.execute(new Runnable() {
//                @Override
//                public void run() {
//                    try {
//                        countDownLatch.await();
//                    } catch (InterruptedException e) {
//                        e.printStackTrace();
//                    }
//                    if(atomicInteger.incrementAndGet() <= 100){
//                        try {
//                            Thread.sleep(100);
//                        } catch (InterruptedException e) {
//                            e.printStackTrace();
//                        }
//                        vals.add(1);
//                    } else {
//                        atomicInteger.decrementAndGet();
//                    }
//                    countDownLatch2.countDown();
//                    countDownLatch3.countDown();
//                }
//            });
//        }
//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                try {
//                    countDownLatch3.await();
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
//                for(int i = 0;i<5;i++){
//                    atomicInteger.decrementAndGet();
//                    vals.poll();
//                }
//                countDownLatch4.countDown();
//            }
//        }).start();
//        Thread.sleep(2000);
//        countDownLatch.countDown();
//        long starttime = System.currentTimeMillis();
//        countDownLatch2.await();
//        countDownLatch4.await();
//        System.out.println("time:"+(System.currentTimeMillis()-starttime));
//        System.out.println(atomicInteger.get());
//    }


}
